package com.team233.zuccecho.util;

import com.team233.zuccecho.Bean.Questionnaire;
import com.team233.zuccecho.rabbitMQ.RPCConsumer;
import com.team233.zuccecho.rabbitMQ.ZuccEchoMessage;
import com.team233.zuccecho.repository.QuestionnaireRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Component
public class ZuccEchoTImer {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private QuestionnaireRepository questionnaireRepository;
    @Autowired
    private AmqpTemplate mqService;
    @Qualifier("rpc")
    @Autowired
    private DirectExchange rpcExchage;

    private final Logger logger = LoggerFactory.getLogger(ZuccEchoTImer.class);


//    @Scheduled(cron = "* * */2 * * ?")
    @Scheduled(cron = "*/5 * * * * ?")/*每隔六秒钟执行一次*/
    public void timer(){
        Timestamp timestamp = new Timestamp(Calendar.getInstance().getTimeInMillis());
        for(Questionnaire qn:questionnaireRepository.findAll()){
            if(timestamp.before(qn.getEndTime())){
                int h = (int) ((qn.getEndTime().getTime()-timestamp.getTime())/(1000*60*60));
                if(h>2){
                    if (redisTemplate.opsForList().range("questionnaire-" + qn.getQuestionnaireID() + ":otherNumber", 0, -1).size() != 0) {
                        //模拟发送通知
                        System.out.println("暂未填写问卷学生：" + redisTemplate.opsForList().range("questionnaire-" + qn.getQuestionnaireID() + ":otherNumber", 0, -1));
                    }
                }else{
                    //RPC模式 确认送达
                    if (redisTemplate.opsForList().range("questionnaire-" + qn.getQuestionnaireID() + ":otherNumber", 0, -1).size() != 0) {
                        System.out.println("暂未填写问卷学生：" + redisTemplate.opsForList().range("questionnaire-" + qn.getQuestionnaireID() + ":otherNumber", 0, -1));
                        List<Long> list = redisTemplate.opsForList().range("questionnaire-" + qn.getQuestionnaireID() + ":otherNumber", 0, -1);
                        for(Long l:list){
                            ZuccEchoMessage msg = new ZuccEchoMessage(ZuccEchoMessage.CATEGORY_RPC);
                            msg.appendContent("msg", "send qn to stu_id"+l+" msg-need-confirm.");
                            logger.warn("send msg to [{}]", rpcExchage.getName());
                            String ret = (String)mqService.convertSendAndReceive(rpcExchage.getName(), Constants.KEY_RPC, msg);
                            logger.warn("send msg got reponse[{}]", ret);
                        }
                    }
                }
            }
        }
    }

}
